package com.bdqn.controller;


import com.bdqn.entity.UmsUser;
import com.bdqn.entity.util.RespBean;
import com.bdqn.service.UmsUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 用户表 前端控制器
 * </p>
 *
 * @author hunter
 * @since 2022-04-30
 */
@RestController
@RequestMapping("/ums/user")
public class UmsUserController {
    @Autowired
    private UmsUserService umsUserService;

    //获取所有用户列表
    @GetMapping("/userList")
    public RespBean userList() {
        List<UmsUser> list = umsUserService.list();
        if (list.size() > 0) {
            return RespBean.ok("查询用户列表成功", list);
        } else {
            return RespBean.err("查询用户列表失败");
        }
    }

    //添加一个用户
    @PostMapping("/addUser")
    public RespBean addUser(@RequestBody UmsUser umsUser) {
        System.out.println("获取的用户对象为" + umsUser);
        boolean save = umsUserService.save(umsUser);
        if (save) {
            return RespBean.ok("添加用户信息成功", true);
        } else {
            return RespBean.err("添加用户信息失败");
        }
    }

    //删除一个用户
    @DeleteMapping("/deleteUser/{id}")
    public RespBean deleteUser(@PathVariable Integer id) {
        if (umsUserService.removeById(id)) {
            return RespBean.ok("删除用户列表成功", true);
        } else {
            return RespBean.err("删除用户列表失败");
        }
    }

    //通过用户电机的id进行查询指定的user对象
    @GetMapping("getUser/{id}")
    public RespBean getUser(@PathVariable int id) {
        UmsUser umsUser = umsUserService.getById(id);
        if (umsUser != null) {
            return RespBean.ok("查询user成功", umsUser);
        } else {
            return RespBean.err("查询user失败");
        }
    }

    //通过用户电机的id进行查询指定的user对象
    @PostMapping("/updateUser")
    public RespBean updateUser(@RequestBody UmsUser umsUser) {
        boolean flag = umsUserService.saveOrUpdate(umsUser);
        if (flag) {
            return RespBean.ok("修改user成功", flag);
        } else {
            return RespBean.err("修改user失败");
        }
    }

    //通过用户名和密码完成登录功能
    @PostMapping("/login")
    public RespBean loginUser(String userName,String password) {
        String token = umsUserService.loginUser(userName, password);
        Map<String, Object> map = new HashMap<>();
        map.put("token", token);
        map.put("username", userName);
        if (token != null) {
            return RespBean.ok("登录成功", map);
        } else {
            return RespBean.err("登录失败");
        }
    }

    //完成退出功能
    @GetMapping("/logout")
    public RespBean loginOut(HttpServletRequest request) {
        String token = request.getHeader("token");

        //拼接上前缀   这样前台传的话就不用传前缀了
        Boolean flag=umsUserService.loginOut(token);
        if (flag==true){
            return RespBean.ok("退出成功",null);
        }else {
            return RespBean.err("退出失败");
        }
    }
    //通过token获取对应的user对象
    @GetMapping("/getUserByToken")
    public RespBean getUserByToken( HttpServletRequest request){
        String token = request.getHeader("token");
        UmsUser umsUser=umsUserService.getUserByToken(token);
        Map<String,UmsUser> map=new HashMap<>();
        map.put("user",umsUser);
        if(umsUser==null){
            //没从redis差出来
            return RespBean.err("通过token获取用户信息失败");
        }else {
            return RespBean.ok("通过token获取用户信息成功",map);
        }
    }
}
